<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="rules" :model="platformForm" label-position="left" label-width="140px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="platformForm.title" />
      </el-form-item>
      <el-form-item label="图标">
        <Upload v-model="platformForm.logo" />
      </el-form-item>
      <el-form-item label="类型">
        <el-radio v-model="platformForm.style.value" label="1">新闻</el-radio>
        <el-radio v-model="platformForm.style.value" label="2">活动</el-radio>
        <el-radio v-model="platformForm.style.value" label="3">产品</el-radio>
        <el-radio v-model="platformForm.style.value" label="4">优惠卷</el-radio>
      </el-form-item>

      <el-form-item label="域名" prop="domain">
        <el-input v-model="platformForm.domain" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="platformForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        保存并发布
      </el-button>
      <el-button v-if="platformForm.status.value <= 5" type="primary" @click="draftData()">
        保存
      </el-button>
      <el-button v-if="platformForm.ID > 0 && platformForm.status.value < 6" type="danger" @click="handleDelete(platformForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { savePlatform, removePlatformByID, getPlatform } from '@/api/popularize/platform'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'
import Upload from '@/components/Upload/SingleImage2' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Upload },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      catalogeOptions: [],
      platformForm: {
        title: '',
        link: '',
        remark: '',
        status: {
          label: '',
          value: 1
        },
        style: {
          label: '',
          value: 1
        }
      },
      rules: []
    }
  },
  created() {
    let platformID = this.$route.query && this.$route.query.platformID
    if (platformID !== undefined) {
      platformID = parseInt(platformID)
      if (!isNaN(platformID)) {
        this.listLoading = true
        const formData = {
          'platformID': platformID
        }
        getPlatform(formData).then(response => {
          if (response.status === 200) {
            this.platformForm = response.data
            this.platformForm.style.value = String(this.platformForm.style.value)
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(platformID)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'platform' })
    },
    createData() {
      this.listLoading = true

      const _this = this
      let formData = {
      }
      formData = Object.assign(formData, _this.platformForm)
      formData.status = 6
      formData.style = formData.style.value

      if (formData.ID > 0) {
        formData.platformID = formData.ID
      }
      savePlatform(formData).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'platform' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    draftData() {
      this.listLoading = true

      const _this = this
      let formData = {
      }
      formData = Object.assign(formData, _this.platformForm)
      formData.status = 1
      formData.style = formData.style.value

      if (formData.ID > 0) {
        formData.platformID = formData.ID
      }
      savePlatform(formData).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'platform' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      this.listLoading = true

      removePlatformByID(this.listQuery).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'platform' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
